"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lane.chang on '28/05/2024'
    comment: 用户意图接口
"""
from fastapi import APIRouter, Request, Body
from typing import Any

from config import Config
from project.lib.decorator import api_common, exec_duration
from project.core.function import json_response
from project.middleware.chatgpt import ChatGPT
from project.service.common import CommonService

gpt_router = APIRouter(prefix='', tags=['ChatGPT'])


@gpt_router.post('/gpt/user_date', summary='用户日期')
@api_common(doc_replace=(('{0}', Config.HTTP_DOC_FIX),))
@exec_duration(add_return=True)
async def inference_user_date(request: Request,
                              text: str = Body(..., title='文本'),
                              _na: Any = Body(None, title='多余参数', description='此参数是针对fastapi框架的bug特制的无效参数,请无视'),):
    """
    **desc**: 推理用户日期

    **url**: http(s)://{0}.udicaria.com/gpt/user_date

    **method**: POST

    **params**:

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: {
                user_date: 用户日期
            }
            exec_time: 接口后端处理时长
        }

    """
    # chat_gpt = ChatGPT()
    # result = chat_gpt.inference_user_date(text)
    data = await CommonService.inference_user_date_async(user_message=text)

    return json_response(data={'user_date': data.get('result', [])})


@gpt_router.post('/gpt/sleep_data_analysis', summary='睡眠数据分析')
@api_common(doc_replace=(('{0}', Config.HTTP_DOC_FIX),))
@exec_duration(add_return=True)
async def do_sleep_data_analysis(request: Request,
                                 system_text: str = Body(..., title='系统文本'),
                                 user_text: str = Body(..., title='用户文本'),
                                 _na: Any = Body(None, title='多余参数', description='此参数是针对fastapi框架的bug特制的无效参数,请无视'),):
    """
    **desc**: 推理用户日期

    **url**: http(s)://{0}.udicaria.com/gpt/sleep_data_analysis

    **method**: POST

    **params**:

        <body>:
            system_text: 系统文本
            user_text: 用户文本

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: {
                analysis_result: 数据分析结果
            }
            exec_time: 接口后端处理时长
        }

    """
    chat_gpt = ChatGPT()
    result = chat_gpt.llm(system_text=system_text, user_text=user_text)

    return json_response(data={'result': result})
